/* watrayucprojectmanager.vala
 *
 * Copyright (C) 2009  Matias De la Puente
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Matias De la Puente <mfpuente.ar@gmail.com>
 */
using Watray;
using Gtk;

public class WatrayUC.ProjectManager: GLib.Object
{
	private IMainWindow _main_window;
	private IProjectsPanel _projects_panel;
	private ActionGroup _action_group;
	
	private uint _ui_id;
	private const string _ui = """
	<ui>
		<menubar name="MainMenu">
			<menu name="FileMenu" action="FileMenuAction">
				<menu name="NewMenu" action="NewMenuAction">
					<placeholder name="NewMenuOps">
						<menuitem action="NewUCProjectAction"/>
					</placeholder>
				</menu>
				<menu name="OpenMenu" action="OpenMenuAction">
					<placeholder name="OpenMenuOps">
						<menuitem action="OpenUCProjectAction"/>
					</placeholder>
				</menu>
			</menu>
		</menubar>
		<popup name="NewPopup" action="NewPopupAction">
			<placeholder name="NewPopupOps">
				<menuitem action="NewUCProjectAction"/>
			</placeholder>
		</popup>
		<popup name="OpenPopup" action="OpenPopupAction">
			<placeholder name="OpenPopupOps">
				<menuitem action="OpenUCProjectAction"/>
			</placeholder>
		</popup>
		<popup name="UCProjectPopup" action="UCProjectPopupAction">
			<menuitem action="NewUCFileAction"/>
		</popup>
	</ui>""";
	
	private const ActionEntry[] _action_entries =
	{
		{ "NewUCProjectAction", STOCK_DIRECTORY, N_("uC project"), null, null, on_new},
		{ "OpenUCProjectAction", STOCK_DIRECTORY, N_("uC project"), null, null, on_open},
		{ "UCProjectPopupAction" },
			{ "NewUCFileAction", STOCK_FILE, N_("Create source file"), null, null, on_new_source }
	};
	
	public ProjectManager (IMainWindow main_window, IProjectsPanel projects_panel)
	{
		_main_window = main_window;
		_projects_panel = projects_panel;
		
		_action_group = new ActionGroup ("WatrayUCActions");
		_action_group.set_translation_domain (Config.GETTEXT_PACKAGE);
		_action_group.add_actions (_action_entries, this);
		
		var ui_manager = _main_window.get_ui_manager ();
		ui_manager.insert_action_group (_action_group, -1);
		_ui_id = ui_manager.add_ui_from_string (_ui, -1);
	}
	
	~DocumentManager ()
	{
		var ui_manager = _main_window.get_ui_manager ();
		ui_manager.remove_ui (_ui_id);
	}
	
	public void on_new ()
	{
		var dir_dialog = new FileChooserDialog (_("Select folder"), null, FileChooserAction.SELECT_FOLDER);
		dir_dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL);
		dir_dialog.add_button (STOCK_OPEN, ResponseType.OK);
		dir_dialog.set_default_response (ResponseType.OK);
		if (dir_dialog.run () == ResponseType.OK)
		{
			var project = new Project ("");
			var prj_dialog = new ProjectDialog (_("New project"), project);
			if (prj_dialog.run () == ResponseType.OK)
			{
				project.filename = Path.build_filename (dir_dialog.get_filename (), project.name + ".watrayuc");
				project.save ();
				_projects_panel.add_project (project);
				project.activated += on_project_activated;
				project.set_item_menu ("/", (Menu)_main_window.get_ui_manager ().get_widget ("/UCProjectPopup"));
			}
			prj_dialog.destroy ();
		}
		dir_dialog.destroy ();
	}
	
	public void on_open ()
	{
		var dialog = new FileChooserDialog (_("Open project"), null, FileChooserAction.OPEN);
		dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL);
		dialog.add_button (STOCK_OPEN, ResponseType.OK);
		dialog.set_default_response (ResponseType.OK);
		if (dialog.run () == ResponseType.OK)
		{
			var project = new Project ("");
			project.filename = dialog.get_filename ();
			project.open ();
			_projects_panel.add_project (project);
			project.refresh ();
			project.activated += on_project_activated;
			project.set_item_menu ("/", (Menu)_main_window.get_ui_manager ().get_widget ("/UCProjectPopup"));
		}
		dialog.destroy ();
	}
	
	public void on_new_source ()
	{
		
	}
	
	private void on_project_activated (Project project)
	{
		var dialog = new ProjectDialog (_("Configure project"), project);
		if (dialog.run () == ResponseType.OK)
		{
			project.save ();
		}
		dialog.destroy ();
	}
}
